package net.xy;

import io.minio.MinioClient;
import io.minio.policy.PolicyType;
import net.xy.utils.Tools;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 描述：
 *
 * @Author shf
 * @Date 2019/5/7 21:59
 * @Version V1.0
 **/
public class Main {

    static MinioClient minioClient;

    static String endpoint;
    static String accessKey;
    static String secretKey;
    static String bucketName;
    static String path;

    static {
        try {
            Properties properties = new Properties();
            properties.load(Main.class.getClassLoader().getResourceAsStream("config.properties"));
            endpoint = properties.getProperty("min-io.oss.endpoint");
            accessKey = properties.getProperty("min-io.oss.access-key");
            secretKey = properties.getProperty("min-io.oss.secret-key");
            bucketName = properties.getProperty("min-io.oss.bucket-name");
            path = properties.getProperty("min-io.oss.path");
            minioClient = new MinioClient(endpoint, accessKey, secretKey);
        } catch (Exception e) {
            throw new ExceptionInInitializerError("load properties error!");
        }
    }

    /**
     * 上传文件
     *
     * @param stream
     * @param path
     * @param filename
     * @param contentType
     * @return
     */
    static String upload(InputStream stream, String path, String filename, String contentType) {
        String s = null;
        try {
            //存入bucket不存在则创建，并设置为只读
            if (!minioClient.bucketExists(bucketName)) {
                minioClient.makeBucket(bucketName);
                minioClient.setBucketPolicy(bucketName, "*.*", PolicyType.READ_WRITE);
            }

            //文件存储的目录结构
            String objectName = Tools.trimPath(path) + filename;
            minioClient.putObject(bucketName, objectName, stream, contentType);
            System.out.println("文件上传成功!");
            s = endpoint + "/" + bucketName + "/" + objectName;
        } catch (Exception e) {
            System.out.println("上传发生错误！");
        }
        return s;
    }

    static InputStream download(String path, String filename) {

        try {
            InputStream object = minioClient.getObject(bucketName, path + "/" + filename);
            return object;
        } catch (Exception e) {
            System.out.println("导出失败！");
        }
        return null;
    }

    public static void main(String[] args) throws IOException {

        String filePath = path + "/test.txt";
        String desFilePath = path + "/" + System.currentTimeMillis() + ".txt";

        String storeName = System.currentTimeMillis() + ".txt";
        System.out.println(storeName);
        upload(new FileInputStream(filePath), "test", storeName, "text/plain");

        InputStream stream = download("test", storeName);
        FileUtils.copyInputStreamToFile(stream, new File(desFilePath));
    }
}
